package br.com.lh.urabuilder.view
{
	import br.com.lh.urabuilder.util.CoordinatesUtils;
	
	import flash.display.Graphics;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import mx.core.UIComponent;
	

	public class RectangleSelectionView
	{
		private var componentCanvas:UIComponent;
		public var auxComponent:UIComponent; 
		
		private var initRectX:uint;
		private var initRectY:uint;
		
		public function RectangleSelectionView(componentCanvas:UIComponent){
			this.componentCanvas = componentCanvas;
				
		}
		
		public function initRectangleSelection(event:MouseEvent):void{
            	initRectX = event.localX;
            	initRectY = event.localY;
            	auxComponent = new UIComponent();
            	componentCanvas.addChild(auxComponent);
            }
            
            public function endRectangleSelection():void{
            	if (auxComponent !=null){
	            	auxComponent.graphics.clear();
	            	auxComponent = null;
            	}
            }
            
            public function drawRectangleSelection(event:MouseEvent):Rectangle{
            	
				auxComponent.alpha = 0.2;
				var graphics:Graphics = auxComponent.graphics;            	
            	var iX:int = initRectX;
            	var iY:int = initRectY;
				
            	graphics.clear();
            	
            	var currentX:Number = event.stageX - CoordinatesUtils.getCanvasLocalToGlobalCoordinates().x; 
            	var currentY:Number = event.stageY - CoordinatesUtils.getCanvasLocalToGlobalCoordinates().y;
            	
            	if (event.localX < initRectX ){
            		iX = event.stageX;
            		currentX = initRectX;
            		 	
            	}
            	if (event.localY < initRectY){
            		iY = currentY;
            		currentY = initRectY;
            		 			
            	}
            	graphics.lineStyle(4,0x22FF22);
            	graphics.beginFill(0x22FF22);
            	graphics.drawRect(iX,iY,currentX - iX,currentY - iY);
            	graphics.endFill();
            	graphics.lineStyle(0,0xDDFFDD);
            	graphics.beginFill(0xDDFFDD);
            	
            	graphics.drawRect(iX + 2,iY + 2,currentX - iX - 4,currentY - iY - 4);
            	graphics.endFill();
				
				return new Rectangle(iX + 2,iY + 2,currentX - iX - 4,currentY - iY - 4);
		    }
		
	}
}